<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/4/24
 * Time: 16:46
 */

namespace app\admin\controller;

use app\admin\model\Sysmsg as SysmsgModel;

class Sysmsg extends Base
{
    private $model;

    public function _initialize()
    {
        $this->model = new SysmsgModel();
    }

    public function index()
    {
        if (request()->isAjax()) {
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $where['status'] = 0;
            $selectResult = $this->model->getUserByWhere($where, $offset, $limit);
//            dump($selectResult);exit;
            $status = config('code.status_normal');

            //拼接参数
            foreach ($selectResult as $k => $v) {
                $selectResult[$k]['status'] = $status[$v['status']];
//                $selectResult[$k]['url'] = '<img src="' . $v['url'] . '" width="40px" height="40px">';
                $selectResult[$k]['operate'] = showOperate($this->makeButton($v['id']));
            }

            $result['total'] = $this->model->getAllUser($where);//总数据
//            dump($selectResult);exit;
            $result['rows'] = $selectResult;
//            dump($result);die;
            return json($result);
        }
        return $this->fetch();
    }

    /***
     * 添加管理员
     * @return mixed|\think\response\Json
     */
    public function add(){
        if (request()->isPost()){
            $param = input('post.');
            $title = trim($param['title']);
            $content = trim($param['content']);
            if(empty($title)) return json(msg('-1','','标题不能为空'));
            if(empty($content)) return json(msg('-1','','内容不能为空'));
            $flag = $this->model->insertMsg($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $this->assign('status',config('sysmsg_status'));
        return $this->fetch();
    }

    public function edit()
    {
        if (request()->isPost()) {
            $param = input('post.');
            $flag = $this->model->editMsg($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');
        $data = $this->model->getOneUser($id);

        $this->assign([
            'data' => $data,
            'status'=>config('sysmsg_status')
        ]);
        return $this->fetch();
    }





    public function del()
    {
        if ($this->request->isGet()) {
            $id = input('param.id');

            $flag = $this->model->saveStatus($id);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'Sysmsg/edit',
                'href' => url('Sysmsg/edit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'Sysmsg/del',
                'href' => "javascript:adminDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}